<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :destroy-on-close="true"
    width="550px"
    :close-on-click-modal="false">
    <el-form
      :model="form"
      :rules="rule"
      ref="form"
      style="width: 450px"
      label-width="120px"
    >
      <el-form-item label="分类名称：" prop="name">
        <el-input placeholder="请输入内容" v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="任务最低数量：" prop="min_num">
        <el-input placeholder="请输入数量" v-model.number="form.min_num"></el-input>
      </el-form-item>
      <el-form-item label="任务最低单价：" prop="min_price">
        <el-input placeholder="请输入单价" v-model="form.min_price"></el-input>
      </el-form-item>
      <el-form-item label="状态：" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio :label="0">显示</el-radio>
          <el-radio :label="1">隐藏</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" style="display: flex;justify-content: center;">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { create, edit } from '../api'

export default {
  name: 'banner-edit',
  data () {
    return {
      dialogVisible: false,
      id: 'add',
      refresh: false,
      form: {
        name: '',
        min_num: '',
        min_price: '',
        status: 0
      },
      rule: {
        name: [
          { required: true, message: '请输入名称' }
        ],
        min_num: [
          { required: true, message: '请输入任务最低数量' }
        ],
        min_price: [
          { required: true, message: '请输入任务最低单价' },
          { validator: this.compareNumber, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.id === 'add' ? '新增' : '修改'
    }
  },
  methods: {
    compareNumber ({ field }, value, callback) {
      const v = parseFloat(this.form[field])
      console.log(v)
      if (v < 0 || (v * 100).toString().includes('.')) {
        return callback(new Error('请输大于0的整数或2位以内小数'))
      }
      return callback()
    },
    handleClose (done) {
      done()
    },
    open (row = false) {
      if (row !== false) {
        const { id, min_price } = row
        this.id = id
        Object.assign(this.form, { ...row, min_price: min_price / 100 })
      } else {
        this.id = 'add'
        Object.assign(this.form, {
          name: '',
          min_num: '',
          min_price: '',
          status: 0
        })
        this.refresh = false
      }
      this.dialogVisible = true
      return new Promise((resolve) => {
        this._resolve = resolve
      })
    },
    cancel () {
      this.close()
    },
    close () {
      this._resolve({ refresh: this.refresh })
      this.dialogVisible = false
    },
    async submit () {
      const params = { ...this.form, min_price: this.form.min_price * 100 }
      if (this.id !== 'add') {
        await edit(this.id, params)
        this.$message.success('更新成功')
      } else {
        await create(params)
        this.$message.success('添加成功')
      }
      this.refresh = true
      this.close()
    }
  }
}
</script>

<style scoped>

</style>
